var tableId = "";
	var tableName = "";
	$(function(){
		var args = window.location.search;
		tableId = args.split("=")[1];
		$.ajax({
			type : 'get',
			dataType: 'json',
			contentType: "application/json",
		    url: ctx + '/sti.do?action=5&tableId=' + tableId,
		    success: function(data) {		    	
		    	tableName = data.tableName;
		    }  
		});
		$.ajax({
			type : 'post',
			dataType: 'json',
			contentType: "application/json",
		    url: ctx + '/stf.do?action=0&tableId=' + tableId,
		    success: function(data) {
		    	var chkhtml = "<table>";
		    	$.each(data.rows, function(i,v){
		    		if(i == 0){
		    			chkhtml += htmlCheckBoxStart(v.fieldEnglishName, v.fieldType);
		    		}else{		    		
			    		if(i % 6 == 0){
			    			chkhtml += htmlCheckBoxTr(v.fieldEnglishName, v.fieldType);
			    		}
			    		
			    		if(i % 6 != 0){
			    			chkhtml += htmlCheckBox(v.fieldEnglishName, v.fieldType);
			    		}
		    		}
		    		
		    	
		    	});
		    	
		    	chkhtml += "</table>";
		    	
		    	$("#divChks").html(chkhtml);
		    	$("#divChks1").html(chkhtml);
				
		    }  
		}); 
	});

	function htmlCheckBoxStart(fieldName, fieldType){
		var html = "";
		html += "<tr><td><input type=\"checkbox\" value=\"" + fieldName + "\"/><span>" + fieldName + "</span><span style=\"display:none;\">" + fieldType + "</span></td>";
		return html;
	}
	
	function htmlCheckBox(fieldName, fieldType){
		var html = "";
		html += "<td><input type=\"checkbox\" value=\"" + fieldName + "\"/><span>" + fieldName + "</span><span style=\"display:none;\">" + fieldType + "</span></td>";
		return html;
	}
	function htmlCheckBoxTr(fieldName, fieldType){
		var html = "";
		html += "</tr><tr><td><input type=\"checkbox\" value=\"" + fieldName + "\"/><span>" + fieldName + "</span><span style=\"display:none;\">" + fieldType + "</span></td>";
		return html;
	}
	
	
	function btnReturn(){
		window.location.href = ctx + "/admin/sql/design.jsp?tableId="+tableId;
	}
	
	function btnCondition(){
		var trObj = $("#divChks1 table tr");
		var trLength = $("#divChks1 table tr").length;
		var condition = "where ";
		for(var i = 0; i< trLength;i++){
			var tdObj = trObj.eq(i).find("td");
			var tdLength = tdObj.length;
			for(var j = 0; j< tdLength;j++){
				var inputObj = tdObj.eq(j).find("input");
				
				if(inputObj.attr("checked")== "checked"){
					condition += inputObj.val() + "='123456789' and ";
				}
			}
		}
		
		if(condition != "where "){
			condition = condition.substring(0, condition.length - 4);
			$("#txtCondition").val(condition);			
		}
	}
	
	function btnClearSql(){
		$("#divShow").html('');
	}
	function clearTxt(){
		$("#txtCondition").val('');
	}
	function btnInsert(){
		var trObj = $("#divChks table tr");
		var trLength = $("#divChks table tr").length;
		var insertSql = "INSERT " +tableName+"(";
		var valueSql = "VALUES(";
		for(var i = 0; i< trLength;i++){
			var tdObj = trObj.eq(i).find("td");
			var tdLength = tdObj.length;
			for(var j = 0; j< tdLength;j++){
				var inputObj = tdObj.eq(j).find("input");
				
				if(inputObj.attr("checked")== "checked"){
					insertSql += inputObj.val() + ",";
					valueSql += "?,";
				}
			}
		}
		insertSql = insertSql.substring(0, insertSql.length - 1) + ") ";
		valueSql = valueSql.substring(0, valueSql.length - 1) + "); ";
		var sql = insertSql + valueSql;
		var noEqual = "INSERT " +tableName+") VALUES) ";
		
		if(sql != noEqual){
			$("#divShow").html(sql);			
		}
	}
	
	function clickAllSelect(){
		var length = $("#divChks table tr td input:checkbox").length;
		for(var i=0;i<length;i++){
			$("#divChks table tr td input:checkbox").eq(i).attr("checked","checked");
		}
	}
	function clickAllSelect1(){
		var length = $("#divChks1 table tr td input:checkbox").length;
		
		if($(this).attr("checked")== "checked"){
			for(var i=0;i<length;i++){
				$("#divChks1 table tr td input:checkbox").eq(i).attr("checked","unchecked");
			}
		}else{
			for(var i=0;i<length;i++){
				$("#divChks1 table tr td input:checkbox").eq(i).attr("checked","checked");
			}
		}
	}
	
	function btnSelect(){
		var trObj = $("#divChks table tr");
		var trLength = $("#divChks table tr").length;
		var selectSql = "SELECT ";
		for(var i = 0; i< trLength;i++){
			var tdObj = trObj.eq(i).find("td");
			var tdLength = tdObj.length;
			for(var j = 0; j< tdLength;j++){
				var inputObj = tdObj.eq(j).find("input");
				
				if(inputObj.attr("checked")== "checked"){
					selectSql += inputObj.val() + ",";
				}
			}
		}
		selectSql = selectSql.substring(0, selectSql.length - 1) + " FROM " + tableName + " ";
		
		var txtCondition = $("#txtCondition").val();
		if(txtCondition != ""){
			selectSql += txtCondition;
		}
		
		$("#divShow").html(selectSql);			

	}
	
	function btnUpdate(){
		var trObj = $("#divChks table tr");
		var trLength = $("#divChks table tr").length;
		var selectSql = "UPDATE " + tableName + " SET ";
		for(var i = 0; i< trLength;i++){
			var tdObj = trObj.eq(i).find("td");
			var tdLength = tdObj.length;
			for(var j = 0; j< tdLength;j++){
				var inputObj = tdObj.eq(j).find("input");
				
				if(inputObj.attr("checked")== "checked"){
					selectSql += inputObj.val() + "='136',";
				}
			}
		}
		selectSql = selectSql.substring(0, selectSql.length - 1) + " ";
		
		var txtCondition = $("#txtCondition").val();
		if(txtCondition != ""){
			selectSql += txtCondition;
		}
		
		$("#divShow").html(selectSql);	
	}